package com.example.drug.mapper;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.example.drug.dto.resp.OrderPatients;
import com.example.drug.dto.resp.OrderPatientsInfo;
import com.example.drug.dto.resp.PatientsInfo;
import com.example.drug.dto.resp.PharmacistDetail;
import com.example.drug.dto.resp.PharmacistOrder;
import com.example.drug.dto.resp.Prescription;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

public interface OrderRxMainMapper {

    @Select(value = "select a.rx_order_no_nvarchar as RxOrderNO,c.name as DoctorName,c.title as Title,a.d_type_tinyint as DType,"
            + "a.chk_status_tinyint as ChkStatus,a.create_time_datetime as CreateTime,d.name_nvarchar as DepartmentName"
            + " from order_rx_main a"
            + " inner join order_main b on a.order_no_nvarchar = b.order_no_nvarchar"
            + " inner join doctor_base_info c on b.dc_id_bigint = c.dc_id"
            + " inner join h_hospital_department d on c.depart_id = d.id_bigint"
            + " where a.chk_dc_id_bigint =  #{dcid} and a.chk_status_tinyint = 0 and a.is_recipe_doc_ca_bit = 1"
            + " order by a.create_time_datetime desc" +
            " limit #{skip},#{limit}")
    List<PharmacistOrder> getPharmacistMsg(@Param("dcid") Long dcid, @Param("skip") Integer skip, @Param("limit") Integer limit);

    @Select(value = "select count(*) "
            + " from order_rx_main a"
            + " inner join order_main b on a.order_no_nvarchar = b.order_no_nvarchar"
            + " inner join doctor_base_info c on b.dc_id_bigint = c.dc_id"
            + " inner join h_hospital_department d on c.depart_id = d.id_bigint"
            + " where a.chk_dc_id_bigint =  #{dcid} and a.chk_status_tinyint = 0 and a.is_recipe_doc_ca_bit = 1"
            + " order by a.create_time_datetime desc ")
    long countPharmacistMsg(@Param("dcid") Long dcid);

    @Select(value = "select a.rx_order_no_nvarchar as RxOrderNO, a.dis_ids_nvarchar as DialecticalCode,a.order_no_nvarchar as OrderNO,a.chk_doctor_nvarchar as ChkDoctor,a.chk_status_tinyint as ChkStatus,a.dialectical_nvarchar as Dialectical,c.pid_bigint as PID,"
            + "c.f_hospital_nvarchar as FHospital,c.name_nvarchar as Name,c.sex_nvarchar as Sex,c.age_int as Age,c.f_department_nvarchar as FDepartment,c.f_visit_date_date as FVisitDate,c.f_diagnosis_nvarchar as FDiagnosis,"
            + "a.open_doctor_nvarchar as OpenDoctor,a.sale_price_money as SalePrice,a.a_void_nvarchar as AVoid,a.failure_reason_nvarchar as FailureReason,a.failure_remark_nvarchar as FailureRemark,"
            + "d.drug_type_nvarchar as DrugType,d.does_int as Does,d.total_does_int as TotalDoes,d.drug_info_nvarchar as DrugInfo,d.med_way_nvarchar as MedWay,a.badge_nvarchar as Badge,b.dc_id_bigint as DCID,"
            + "a.create_time_datetime as CreateTime,f.name_nvarchar as depName,a.d_type_tinyint as DType,d.use_time_nvarchar as MedTime, d.day_count_int as DayCount,a.z_dialectical_nvarchar as zDialectical,a.z_dialectics_nvarchar as zDialectics"
            + " from order_rx_main a"
            + " inner join order_main b on a.order_no_nvarchar = b.order_no_nvarchar"
            + " inner join order_patients_info c on b.order_no_nvarchar = c.order_no_nvarchar"
            + " inner join order_rx_info d on a.rx_order_no_nvarchar = d.rx_order_no_nvarchar"
            + " inner join doctor_base_info e on b.dc_id_bigint = e.dc_id"
            + " inner join h_hospital_department f on e.depart_id = f.id_bigint"
            + " where a.chk_dc_id_bigint = #{dcid} and a.rx_order_no_nvarchar = #{RxOrderNO}")
    PharmacistDetail getPharmacistdetails(@Param("dcid") Long dcid, @Param("RxOrderNO") String RxOrderNO);

    @Select(value = "select a.order_no_nvarchar as OrderNO,a.PID_bigint as PID,a.name_nvarchar as Name,a.sex_nvarchar as Sex,a.age_int as Age,"
            + "c.header_nvarchar as Header,a.allergy_nvarchar as Allergy,a.past_disease_nvarchar as PastDisease,a.f_visit_date_date as FVisitDate,b.dc_id_bigint as DCID"
            + " from order_patients_info a"
            + " inner join order_main b on a.order_no_nvarchar = b.order_no_nvarchar"
            + " inner join client_patients_info c on a.PID_bigint = c.pid_bigint"
            + " where a.PID_bigint = #{PID} and a.order_no_nvarchar = #{OrderNO}"
            + " order by a.f_visit_date_date desc")
    PatientsInfo getOrderPatientsInfo(@Param("PID") Long PID, @Param("OrderNO") String OrderNO);

    @Select(value = "select a.order_no_nvarchar as OrderNO,c.name as DoctorName,c.title as Title,"
            + " a.f_department_nvarchar as FDepartment,a.f_diagnosis_nvarchar as FDiagnosis,a.f_visit_date_date as FVisitDate"
            + " from order_patients_info a"
            + " inner join order_main b on a.order_no_nvarchar = b.order_no_nvarchar"
            + " inner join doctor_base_info c on b.dc_id_bigint = c.dc_id"
            + " where a.pid_bigint = #{PID} and b.dc_id_bigint = #{DCID}")
    List<OrderPatientsInfo> getDoctorInfoLists(@Param("PID") Long PID, @Param("DCID") Long DCID);

    @Select(value = "select a.id_bigint as ID,d.id_no_nvarchar as IdCard,a.order_no_nvarchar as OrderNO,a.name_nvarchar as Name,a.sex_nvarchar as Sex,a.age_int as Age,"
            + " a.first_tinyint as First,a.f_hospital_nvarchar as FHospital,c.Name as DoctorName,c.title as Title,a.f_department_nvarchar as FDepartment,"
            + " a.f_diagnosis_nvarchar as FDiagnosis,a.f_visit_date_date as FVisitDate,a.chief_complaint_nvarchar as Chiefcomplaint,"
            + " a.allergy_nvarchar as Allergy,a.past_disease_nvarchar as PastDisease,b.receive_time_datetime as ReceiveTime,"
            + " a.is_pregnancy_bit as isPregnancy,a.is_lactation_period_bit as isLactationPeriod"
            + " from order_patients_info a"
            + " inner join order_main b on a.order_no_nvarchar = b.order_no_nvarchar"
            + " inner join doctor_base_info c on b.dc_id_bigint = c.dc_id"
            + " inner join client_patients_info d on a.pid_bigint=d.pid_bigint"
            + " where a.pid_bigint = #{PID} and a.order_no_nvarchar = #{OrderNO}")
    OrderPatients getOrderPatients(@Param("PID") Long PID, @Param("OrderNO") String OrderNO);

    @Select(value = "select a.rx_order_no_nvarchar as RxOrderNO,c.Name as DoctorName,c.title as Title,a.d_type_tinyint as DType,a.chk_status_tinyint as ChkStatus,"
            + " a.create_time_datetime as CreateTime,a.failure_reason_nvarchar as FailureReason,a.failure_remark_nvarchar as FailureRemark,d.name_nvarchar as DepartmentName"
            + " from order_rx_main a"
            + " inner join order_main b on a.order_no_nvarchar = b.order_no_nvarchar"
            + " inner join doctor_base_info c on b.dc_id_bigint = c.dc_id"
            + " inner join h_hospital_department d on c.depart_id = d.id_bigint"
            + " where a.chk_dc_id_bigint = #{DCID} and a.chk_status_tinyint = #{ChkStatus}"
            + " order by a.create_time_datetime desc"
            + " limit #{Num},#{Limit}")
    List<Prescription> getPrescription(@Param("DCID") Long DCID, @Param("ChkStatus") int ChkStatus, @Param("Num") int Num, @Param("Limit") int Limit);


    @SelectProvider(type = SqlBuilder.class, method = "rxListSQL")
    List<Map<String, Object>> rxList(@Param("dcid") long dcid, @Param("beg") Date beg, @Param("end") Date end, @Param("dType") Byte dType,
                                     @Param("chkStatus") int chkStatus, @Param("name") String name,
                                     @Param("skip") int skip, @Param("limit") int limit);

    @SelectProvider(type = SqlBuilder.class, method = "rxCountSQL")
    int rxCount(@Param("dcid") long dcid, @Param("beg") Date beg, @Param("end") Date end, @Param("dType") Byte dType,
                @Param("chkStatus") int chkStatus, @Param("name") String name);

    class SqlBuilder {
        public String rxListSQL(@Param("dcid") long dcid, @Param("beg") Date beg, @Param("end") Date end, @Param("dType") Byte dType,
                                @Param("chkStatus") int chkStatus, @Param("name") String name,
                                @Param("skip") int skip, @Param("limit") int limit) {
            String sql = "select a.rx_order_no_nvarchar as RxOrderNO,c.Name as DoctorName,a.d_type_tinyint as DType,a.chk_status_tinyint as ChkStatus,e.name_nvarchar as Name,"
                    + " a.create_time_datetime as CreateTime,d.name_nvarchar as DepartmentName"
                    + " from order_rx_main a"
                    + " inner join order_main b on a.order_no_nvarchar = b.order_no_nvarchar"
                    + " inner join doctor_base_info c on b.dc_id_bigint = c.dc_id"
                    + " inner join h_hospital_department d on c.depart_id = d.id_bigint"
                    + " inner join order_patients_info e on b.order_no_nvarchar = e.order_no_nvarchar"
                    + " where a.chk_dc_id_bigint = #{dcid} and a.is_recipe_doc_ca_bit = 1";
            if (beg != null && end != null)
                sql += " and a.create_time_datetime > #{beg} and a.create_time_datetime < #{end}";
            if (name != null && name.length() > 0)
                sql += " and e.name_nvarchar like CONCAT('%',#{name},'%')";
            if (dType != 0)
                sql += " and a.d_type_tinyint = #{dType}";
            if (chkStatus != -1)
                sql += " and a.chk_status_tinyint = #{chkStatus}";
            sql += " order by a.create_time_datetime desc limit #{skip},#{limit}";
            return sql;
        }

        public String rxCountSQL(@Param("dcid") long dcid, @Param("beg") Date beg, @Param("end") Date end, @Param("dType") Byte dType,
                                 @Param("chkStatus") int chkStatus, @Param("name") String name) {
            String sql = "select count(a.rx_order_no_nvarchar)"
                    + " from order_rx_main a"
                    + " inner join order_main b on a.order_no_nvarchar = b.order_no_nvarchar"
                    + " inner join doctor_base_info c on b.dc_id_bigint = c.dc_id"
                    + " inner join h_hospital_department d on c.depart_id = d.id_bigint"
                    + " inner join order_patients_info e on b.order_no_nvarchar = e.order_no_nvarchar"
                    + " where a.chk_dc_id_bigint = #{dcid} and a.is_recipe_doc_ca_bit = 1";
            if (beg != null && end != null)
                sql += " and a.create_time_datetime > #{beg} and a.create_time_datetime < #{end}";
            if (name != null && name.length() > 0)
                sql += " and e.name_nvarchar like CONCAT('%',#{name},'%')";
            if (dType != 0)
                sql += " and a.d_type_tinyint = #{dType}";
            if (chkStatus != -1)
                sql += " and a.chk_status_tinyint = #{chkStatus}";
            return sql;
        }
    }
}
